import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelCity {
    public static void main(String[] args) throws SQLException {
        //1.获得数据源
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        //2.得到连接Connection
        Connection connection= (Connection) dataSource.getConnection();
        //3.获得执行器
        String sql="select * from city where id <?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,10);
        //4.执行SQL,打印查询结果
        ResultSet resultSet=statement.executeQuery();//得到结果集
        while(resultSet.next()){
            City city=new City();
            city.setId(resultSet.getInt("id"));//查询当前行中列名为id的值
            city.setName(resultSet.getString("name"));
            //打印city对象
            System.out.println(city);
        }
        //5.关闭资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
